package cn.iocoder.yudao.module.product.controller.admin.stockoutin;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.stockoutin.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.stockoutin.StockOutInDO;
import cn.iocoder.yudao.module.product.service.stockoutin.StockOutInService;

@Tag(name = "管理后台 - 库存操作")
@RestController
@RequestMapping("/product/stock-out-in")
@Validated
public class StockOutInController {

    @Resource
    private StockOutInService stockOutInService;

    @PostMapping("/create")
    @Operation(summary = "创建库存操作")
    @PreAuthorize("@ss.hasPermission('product:stock-out-in:create')")
    public CommonResult<Long> createStockOutIn(@Valid @RequestBody StockOutInSaveReqVO createReqVO) {
        return success(stockOutInService.createStockOutIn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存操作")
    @PreAuthorize("@ss.hasPermission('product:stock-out-in:update')")
    public CommonResult<Boolean> updateStockOutIn(@Valid @RequestBody StockOutInSaveReqVO updateReqVO) {
        stockOutInService.updateStockOutIn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存操作")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:stock-out-in:delete')")
    public CommonResult<Boolean> deleteStockOutIn(@RequestParam("id") Long id) {
        stockOutInService.deleteStockOutIn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存操作")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:stock-out-in:query')")
    public CommonResult<StockOutInRespVO> getStockOutIn(@RequestParam("id") Long id) {
        StockOutInDO stockOutIn = stockOutInService.getStockOutIn(id);
        return success(BeanUtils.toBean(stockOutIn, StockOutInRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存操作分页")
    @PreAuthorize("@ss.hasPermission('product:stock-out-in:query')")
    public CommonResult<PageResult<StockOutInRespVO>> getStockOutInPage(@Valid StockOutInPageReqVO pageReqVO) {
        PageResult<StockOutInDO> pageResult = stockOutInService.getStockOutInPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockOutInRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存操作 Excel")
    @PreAuthorize("@ss.hasPermission('product:stock-out-in:export')")
    @OperateLog(type = EXPORT)
    public void exportStockOutInExcel(@Valid StockOutInPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockOutInDO> list = stockOutInService.getStockOutInPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存操作.xls", "数据", StockOutInRespVO.class,
                        BeanUtils.toBean(list, StockOutInRespVO.class));
    }

}